<template>
    <el-form ref="form" style="width:400px;" :rules="rules" :model="data" label-width="100px" label-position="right">
        <el-form-item label="组织名称" prop="name">
            <el-input v-model="data.name" style="width:228px"></el-input>
        </el-form-item>
        <el-form-item label="编码" prop="code">
            <el-input v-model.number="data.code" style="width:228px"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="sort">
            <el-input v-model.number="data.sortId" style="width:228px"></el-input>
        </el-form-item>
        <el-form-item label="备注">
            <el-input v-model="data.remark" type="textarea" style="width:228px"></el-input>
        </el-form-item>
        <el-form-item style="margin-top: 30px">
            <el-button style="width:120px" type="primary" @click="OnSubmit">保存</el-button>
            <el-button style="width:120px" @click="Hiden">取消</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
import selectBox from '../../../Common/selectBox.vue'
export default {
    components: {
        selectBox: selectBox
    },
    mounted() {
//      this.ajax({
//          url: 'SystemOrganization/GetNullData?dataType=1',
//          success: function(data) {
//              this.initData = data;
//              if (this.editData != null) {
//                  this.data = this.editData;
//              } else {
//                  this.data = JSON.parse(JSON.stringify(this.initData));
//              }
//          }
//      });
    },
    props: ["btnType", "parentId", 'editData'],
    data() {
        var check = (rule, value, callback) => {
            if (!value) {
                callback()
            } else if (!Number.isInteger(value)) {
                callback(new Error('请输入数值'))
            } else {
                callback()
            }
        }
        return {
            data: {},
            rules: {
                name: [{
                    required: true,
                    message: '请输入组织名称',
                    trigger: 'blur'
                }],
                code: [{
                    required: true,
                    validator: check,
                    type: 'number',
                    message: '编码必须为数字值'
                }],
                sort: [{
                    validator: check,
                    type: 'number',
                    message: '序号必须为数字值'
                }]
            },
            initData: {}
        }
    },
    methods: {
        OnSubmit() {
            this.$refs['form'].validate((valid) => {
                console.log(valid)
                if (valid) {
                    if (this.btnType != -1) { //btnType:1，增加父节点，2，增加子节点，3，编辑
                        let apiAction = this.btnType < 3 ? "addOrg" : "editOrg";
                        if (this.btnType == 2) {
                            this.data.pId = this.parentId;
                            this.data.level=2;
                        }else if(this.btnType == 1){
                        	this.data.pId=0;
                        	this.data.level=1;
                        }
                        this.data.enable=1;
                        this.data.isDelete=1;
                        if(this.btnType<3)this.data.hasChild=0;
                        
                        this.ajax({
                            url: 'tSysOrganization/' + apiAction,
                            type: 'post',
                            data: this.data,
                            success: function(data) {
                                this.name = ''
                                this.code = ''
                                this.sortId = ''
                                this.remark = ''
                                this.Hiden();
                                this.$emit("Refresh");
                            }
                        });
                    } else {
                        //this.$message.error('页面错误')；
                    }
                } else {
                    return false;
                }
            });

        },
        Hiden() {
            this.$emit("Show", false);
        },
        ClearData() {
            this.$refs['form'].resetFields();
            this.data = JSON.parse(JSON.stringify(this.initData));
        }
    },
    watch: {
        editData: function(newVal) {
            this.data = newVal;
        }
    }
}
</script>
